Fix #10315: False positive: Dead catch#5100
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #10315: False positive: Dead catch#5100phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…od with @throws When a class uses a trait with a non-abstract method that has a @throws tag, and the class overrides that method, the @throws tag was not inherited. This caused false positive "Dead catch" errors when implicitThrows was false, because PHPStan did not recognize that the original trait method could throw the exception. The existing trait PHPDoc inheritance in PhpDocInheritanceResolver only processed abstract trait methods (to avoid inheriting @return/@param from non-abstract trait methods). This fix adds a targeted second pass that inherits only @throws tags from non-abstract trait methods when the overriding method has no @throws of its own. Fixes phpstan/phpstan#10315
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes phpstan/phpstan#10315
Summary
@throwstagResolvedPhpDocBlock::withThrowsTag()to allow setting just the throws tag on a resolved PHPDoc blockDetails
When
implicitThrows: falseis configured and a class overrides a non-abstract trait method that has a@throwstag, PHPStan incorrectly reports the catch as dead because the@throwstag is not inherited from the trait method.The root cause is in
PhpDocInheritanceResolver::resolvePhpDocForMethod(). The existing trait loop (lines 112-143) only processes abstract trait methods for full PHPDoc inheritance. This is correct for@returnand@paramtags — overriding a non-abstract trait method should not inherit those. However,@throwstags should still be inherited because the exception contract of the original trait method is still relevant when a catch block wraps a call in a different trait method on the same class.The fix adds a targeted second pass after the abstract-only loop that specifically inherits
@throwstags from non-abstract trait methods, without affecting@return/@paraminheritance.